from mitmproxy import ctx, http
import redis
import time
import os
import datetime

# Redis

host = "magicmall.cc"
port = 6379
db = 0
r = redis.Redis(host=host, port=port, db=db)
redis_key = 'wechat_article_' + datetime.datetime.today().strftime("%Y-%m-%d")

# 日志
log_file_path = './log'
log_file_name = './log/' + datetime.datetime.today().strftime("%Y-%m-%d") + '.log'
if not os.path.exists(log_file_path):
    os.mkdir(log_file_path)
log_f = open(log_file_name, mode='a', encoding='utf-8')


class WeChat:
    def response(self, flow):
        if flow.request.url.startswith('https://mp.weixin.qq.com/mp/profile_ext?action=getmsg'):
            r.rpush(redis_key, flow.response.text)
            log_f.write(flow.response.text + "\n")

    def __del__(self):
        log_f.close()
        r.close()


addons = [
    WeChat()
]
